const app = getApp()
const db = wx.cloud.database()
const _ = db.command
const $ = db.command.aggregate
Page({
  data: {
    tabs: [],
    tabCur: 0
  },
  //顶部选择分类条目
  tabSelect(e) {
    this.setData({
      tabCur: e.currentTarget.dataset.index,
      scrollLeft: (e.currentTarget.dataset.index - 2) * 200
    }, success => {
      this.getList()
    })
  },
  onShow() {
    this.getForum()

  },
  getForum() {
    wx.request({
      url: 'http://localhost:8080/api/forum/category',
      method: 'POST',
      success: res => {
        console.log('分类数据', res.data)
        this.setData({
          tabs: res.data.data
        })
        this.getList()
      },
      fail: err => {
        console.error('Failed to fetch forum data:', err)
      }
    });
  },
  getList() {
    let type = this.data.tabs[this.data.tabCur].type

    wx.request({
      url: 'http://localhost:8080/api/forum/getListByType',
      method: 'POST',
      data: {
        action: 'getList',
        type: type
      },
      success: res => {
        if (res.data.code == 200) {
          console.log('列表成功', res.data)
          this.setData({
            list: res.data.data
          })
        }
      }
    })
    // wx.cloud.callFunction({
    //     name: "forum",
    //     data: {
    //       action: 'getList',
    //       type: type
    //     }
    //   })
    //   .then(res => {
    //     console.log('列表成功', res)
    //     this.setData({
    //       list: res.result.data
    //     })
    //   })
  },
  // 去发布页
  goPublish() {
    wx.navigateTo({
      url: '/pages/publish/publish',
    })
  },
  // 去论坛详情页
  goForumDetail(e) {
    wx.navigateTo({
      url: '/pages/forumDetail/forumDetail?id=' + e.currentTarget.dataset.id,
    })
  }

})